package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.account.dao.TResourceMapper;
import com.woniu.account.dto.ResourceDto;
import com.woniu.account.form.ResourceInsertForm;
import com.woniu.account.model.TResource;
import com.woniu.account.service.TResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TResourceServiceImpl extends ServiceImpl<TResourceMapper, TResource> implements TResourceService {

    @Resource
    private TResourceMapper resourceMapper;
    @Override
    public List<ResourceDto> selectMenu(Integer userId) throws Exception {
        //根据sql语句查询一二级菜单
        List<TResource> list = resourceMapper.selectMenu(userId);
        //遍历list中的二级和一级菜单
        //第一次循环
        ArrayList<ResourceDto> data = new ArrayList<ResourceDto>();
        for(TResource menu:list){
            if(menu.getParentId()==0){
                //说明是一级菜单
                ResourceDto level1Menu = BeanUtil.toBean(menu, ResourceDto.class);
                data.add(level1Menu);

            }
        }
        for(TResource menu:list){
            if(menu.getParentId()!=0){
                //说明是二级菜单
                ResourceDto level2Menu = BeanUtil.toBean(menu,ResourceDto.class);
                for(ResourceDto levelone:data){//将一级菜单进行遍历
                    if(levelone.getResourceId()==menu.getParentId()){
                        levelone.getChildren().add(level2Menu);
                    }

                }
            }
        }
        return data;
    }

    @Override
    public List<ResourceDto> findButton(Integer userId) throws Exception {
        //根据sql语句查询三级按钮
        List<TResource> list = resourceMapper.findButton(userId);
        ArrayList<ResourceDto> data = new ArrayList<ResourceDto>();

        for(TResource menu:list){
            if(menu.getLevel()==3){
                //说明是一级菜单
                ResourceDto level1Menu = BeanUtil.toBean(menu, ResourceDto.class);
                data.add(level1Menu);

            }
        }
        return data;
    }

    @Override
    public List<ResourceDto> search() throws Exception {
        QueryWrapper<TResource> wrapper = new QueryWrapper<TResource>();
        wrapper.eq("parent_id",0);
        List<TResource> list = resourceMapper.selectList(wrapper);
        ArrayList<ResourceDto> dto = new ArrayList<ResourceDto>();
        if(list!=null&&list.size()>0){
            for (TResource resource : list) {
                ResourceDto resourceDto = BeanUtil.toBean(resource, ResourceDto.class);

                dto.add(resourceDto);

                findChildren(resourceDto);
            }
        }

        return dto;
    }
    //递归查询子类
    public void findChildren(ResourceDto resourceDto){
        QueryWrapper<TResource> queryWrapper = new QueryWrapper<TResource>();
        queryWrapper.eq("parent_id",resourceDto.getResourceId());
        List<TResource> list = resourceMapper.selectList(queryWrapper);
        if(list!=null&&list.size()>0){
            for (TResource resource : list) {
                ResourceDto childDto = BeanUtil.toBean(resource, ResourceDto.class);
                resourceDto.getChildren().add(childDto);
                findChildren(childDto);
            }
        }

    }

    @Override
    public TResource insert(ResourceInsertForm form) throws Exception {
        QueryWrapper<TResource> wrapper = new QueryWrapper<TResource>();
        wrapper.eq("resource_name",form.getResourceName());
        wrapper.or();
        wrapper.eq("resource_url",form.getResourceUrl());
        List<TResource> list = resourceMapper.selectList(wrapper);
        if(list.size()>0){
            throw new Exception("资源名称或资源地址重复");
        }
        TResource resource = BeanUtil.toBean(form, TResource.class);
        resourceMapper.insert(resource);
        return resource;
    }
}
